﻿var zombies = zombies || {};

zombies.advtop = function () {
    var self = this;

    self.Loaded = ko.observable(true);
    self.AdvertiseTOPs1 = ko.observableArray();
    self.AdvertiseTOPs2 = ko.observableArray();
    self.VideoTOPs = ko.observableArray();
    self.NO = ko.observable();
    self.AdvId = ko.observable();
    self.CompanyName = ko.observable();
    self.Title = ko.observable();
    self.PreviewImg = ko.observable();
    self.Description = ko.observable();
    self.Views = ko.observable();
    self.Likes = ko.observable();

    self.PreviewImg = ko.observable();
    self.Views = ko.observable();
    self.Likes = ko.observable();

    self.Name = ko.observable();
    self.Email = ko.observable();
    self.Content = ko.observable();

    self.doSendEm = function (formElement) {
        alert('ok, da send email');
    }
}

zombies.VideoTOP = function (item) {
    var self = this;

    self.PreviewImg = item.PreviewImg;
    self.CompanyName = item.CompanyName;
    self.Title = item.Title;
    self.Views = item.Views;
    self.Likes = item.Likes;
}

zombies.AdvertiseTOP = function (item) {
    var self = this;

    self.NO = item.NO;
    self.AdvId = item.AdvId;
    self.CompanyName = item.CompanyName;
    self.Title = item.Title;
    self.PreviewImg = item.PreviewImg;
    self.Description = item.Description;
    self.Views = item.Views;
    self.Likes = item.Likes;
};

var loadAdvTOP = function (id) {
    AdverTOPViewModel.AdvertiseTOPs1.removeAll();
    AdverTOPViewModel.AdvertiseTOPs2.removeAll();

    $.ajax({
        url: '/api/AdvertiseTOP/GetAdvertiseTop/' + id,
        type: 'GET',
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    }).done(function (items) {
        $.each(items, function (idx, item) {
            if (idx % 2 == 0) {
                AdverTOPViewModel.AdvertiseTOPs1.push(new zombies.AdvertiseTOP(item));
            }
            else {
                AdverTOPViewModel.AdvertiseTOPs2.push(new zombies.AdvertiseTOP(item));
            }
        });
    }).error(function () {
        toastr.error('error');
        AdverTOPViewModel.Loaded(false);
    })
};

var loadVideoTOP = function () {
    AdverTOPViewModel.VideoTOPs.removeAll();

    $.ajax({
        url: '/api/AdvertiseTOP/GetVideoTop',
        type: 'GET',
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    }).done(function (items) {
        $.each(items, function (idx, item) {
            if (idx > 0) {
                AdverTOPViewModel.VideoTOPs.push(new zombies.VideoTOP(item));
            }
            else {
                AdverTOPViewModel.PreviewImg(item.PreviewImg);
                AdverTOPViewModel.Views(item.Views);
                AdverTOPViewModel.Likes(item.Likes);
            }
        });
    }).error(function () {
        toastr.error('error');
        AdverTOPViewModel.Loaded(false);
    })
}

var AdverTOPViewModel;
$(document).ready(function () {
    AdverTOPViewModel = new zombies.advtop();
    loadAdvTOP(0);
    loadVideoTOP();
    ko.applyBindings(AdverTOPViewModel, document.getElementById("my-container"));
});